#include <windows.h>

#include "WndUtil.h"

RECT Rect2RECT( Rect& r ) {
	RECT rect = {0};
	rect.left = r.left;
	rect.bottom = r.bottom;
	rect.right = r.right;
	rect.top = r.top;
	return rect;
}
Rect RECT2Rect( RECT r ) {
	Rect r2;
	r2.left = r.left;
	r2.right = r.right;
	r2.top = r.top;
	r2.bottom = r.bottom;
	return r2;
}
WndUtil::WndUtil(HWND w) :
		m_Wnd(w) {

}
HWND WndUtil::getWnd() {
	return m_Wnd;
}
void WndUtil::setWnd(HWND hwnd) {
	m_Wnd = hwnd;
}
WndUtil::operator HWND() {
	return getWnd();
}

Rect WndUtil::getWindowRect() {
	RECT r;
	::GetWindowRect(getWnd(), &r);
	return RECT2Rect(r);
}
Rect WndUtil::getClientRect() {
	RECT r;
	::GetClientRect(getWnd(), &r);
	return RECT2Rect(r);
}

BOOL WndUtil::showWindow(int cmdShow) {
	return ::ShowWindow(getWnd(), cmdShow);
}
BOOL WndUtil::updateWindow() {
	return ::UpdateWindow(getWnd());
}

HWND WndUtil::setFocus() {
	return ::SetFocus(getWnd());
}
BOOL WndUtil::destroyWindow() {
	return ::DestroyWindow(getWnd());
}
BOOL WndUtil::moveWindow(int x, int y, int w, int h, BOOL repaint) {
	return ::MoveWindow(getWnd(), x, y, w, h, repaint);
}
HWND WndUtil::getParentWindow() {
	HWND parent = ::GetParent(getWnd());
	if (parent && IsWindow(parent)) {
		return parent;
	}
	return 0;
}
BOOL WndUtil::isPopup() {
	LONG style = ::GetWindowLong(getWnd(), GWL_STYLE);
	return style & (WS_POPUP | WS_POPUPWINDOW);
}
void WndUtil::fillParentClientArea() {
	HWND parent = getParentWindow();

	if (parent && IsWindow(getWnd()) && !isPopup()) {
		RECT r = { 0 };
		::GetClientRect(parent, &r);
		//::SetWindowPos(child, 0, 0, 0, r.right-r.left, r.bottom-r.top, SWP_NOZORDER|SWP_SHOWWINDOW);
		::MoveWindow(getWnd(), 0, 0, r.right - r.left, r.bottom - r.top, TRUE);
	}
}

BOOL WndUtil::setMenu(HMENU menu) {
	return ::SetMenu(getWnd(), menu);
}
HMENU WndUtil::getMenu() {
	return ::GetMenu(getWnd());
}
//Send WM_CLOSE, not Minimize Window -- API ::CloseWindow(...) will Minimize Window
void WndUtil::closeWindow() {
	sendMessage(WM_CLOSE, 0, 0);
}

HINSTANCE WndUtil::getInstance() {
	return (HINSTANCE) ::GetWindowLong(getWnd(), GWL_HINSTANCE);
}

HRESULT WndUtil::sendMessage(UINT msg, DWORD wParam, DWORD lParam) {
	return ::SendMessage(getWnd(), msg, wParam, lParam);
}
HRESULT WndUtil::postMessage(UINT msg, DWORD wParam, DWORD lParam) {
	return ::PostMessage(getWnd(), msg, wParam, lParam);
}

void WndUtil::operator=(HWND w) {
	m_Wnd = w;
}
